package com.orangelife.mobile.myhouse.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.common.jump.HouseJumper;
import com.curry.android.http.OrangeErrorHandler;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.util.DialogHelper;
import com.curry.android.view.recyclerView.YfListRecyclerView;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.app.application.CurryApplication;
import com.orangelife.mobile.common.view.OneBtnConfirmDialog;
import com.orangelife.mobile.myhouse.adapter.CertOkRvAdapter;
import com.orangelife.mobile.myhouse.biz.HouseBiz;
import com.orangelife.mobile.myhouse.biz.HouseHttpBiz;
import com.orangelife.mobile.myhouse.entity.IndustryBean;
import com.orangelife.mobile.util.ToastHelper;

import org.json.JSONObject;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 该小区的所有房子和停车位
 * Created by 吴格非 on 2015/12/3.
 */
@Deprecated
public class IndustryFragment extends Fragment {

    @Bind(R.id.rvIndustry)
    YfListRecyclerView rvIndustry;
    @Bind(R.id.ivSelectAll)
    ImageView ivSelectAll;
    @Bind(R.id.tvCommit)
    TextView tvCommit;
    private IndustryBean mIb;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frgm_industry, container, false);
        ButterKnife.bind(this, view);
        setAdapter();
        return view;
    }

    @OnClick(R.id.tvCommit)
    public void onClick() {
        request();
    }

    private void request() {
        if (mIb == null || HouseBiz.isSelectedNothing(mIb.getHouseList())) {
            ToastHelper.getInstance()._toast("请选择要认证的产业");
            return;
        }
        showDialog();
        mIb = HouseBiz.getSelectedCommitInfo(mIb);

        HouseHttpBiz.synchOtherCertInfo(mIb, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                dismissDialog();
                boolean isOk = OrangeErrorHandler.getInstance().isSuccess(response, "同步认证失败", "");
                if (isOk) {
                    ShowConfirmDialog();
                }
            }
        });

    }

    public void setData(IndustryBean ib) {
        this.mIb = ib;
    }


    private void setAdapter() {
        if (mIb != null) {
            ArrayList<IndustryBean.House> temp = new ArrayList<>();
            ArrayList<IndustryBean.House> list = mIb.getHouseList();
            if (IndustryBean.HAS_PARKING_SPACE.equals(mIb.getHasParkingSpace())) {
                IndustryBean.House h = new IndustryBean.House();
                h.setApartmentName("停车位");
                h.setSelected(false);
                temp.add(h);
            }
            if (list != null) {
                for (int i = 0; i < list.size(); i++) {
                    temp.add(list.get(i));
                }
            }
            if (temp.size() > 0) {
                mIb.setHouseList(temp);
                CertOkRvAdapter adapter = new CertOkRvAdapter(temp);
                adapter.setImageViewSelectAll(ivSelectAll);
                rvIndustry.setAdapter(adapter);
            }
        }
    }

    private void ShowConfirmDialog() {
        final OneBtnConfirmDialog d = new OneBtnConfirmDialog(getActivity(), "提示", "恭喜您，同步认证成功！", "确定");
        d.show();
        d.setClicklistener(new OneBtnConfirmDialog.ClickListenerInterface() {

            @Override
            public void doConfirm() {
                CurryApplication.getInstance().finishActivity(getActivity());
                HouseJumper.jumpToList(getActivity());
            }
        });
    }

    /*##################dialog Start###################*/
    private Dialog dialog;

    private void showDialog() {
        dialog = DialogHelper.getInstance().createLoadingDialog(getActivity());
        dialog.show();
    }

    private void dismissDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }
     /*##################dialog end###################*/
}
